package org.vacoor.nothing.misc.express.usps.api;

/**
 * USPS 请求结果
 *
 * @author vacoor
 */
public class UspsResult<T> {

    public static interface Builder<T> {
        UspsResult<T> build(UspsRequest req, String responseXml);
    }

    protected UspsRequest request;
    protected T target;
    protected String errorCode;
    protected String errorReason;
    protected String errorSource;
    protected String responseText;

    public <R extends UspsRequest> R getRequest() {
        return (R) request;
    }

    public T getTarget() {
        return target;
    }

    public boolean isSuccess() {
        return null == errorCode || 1 > errorCode.length();
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorReason() {
        return errorReason;
    }

    public String getErrorSource() {
        return errorSource;
    }

    public String getResponseText() {
        return responseText;
    }

    private UspsResult() {
    }

    public static <T> UspsResult<T> failed(UspsRequest req, String responseText, String errorCode, String errorReason, String errorSource) {
        UspsResult<T> result = new UspsResult<T>();
        result.request = req;
        result.responseText = responseText;
        result.errorCode = errorCode;
        result.errorReason = errorReason;
        result.errorSource = errorSource;
        return result;
    }

    public static <T> UspsResult<T> success(UspsRequest req, String responseText, T target) {
        UspsResult<T> result = new UspsResult<T>();
        result.request = req;
        result.responseText = responseText;
        result.target = target;
        return result;
    }
}
